package cn.itcast.model

import org.apache.spark.sql.{DataFrame, SparkSession}

object SparkTest {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
    .appName("sparktest")
    .master("local[*]")
      .getOrCreate()
    import spark.implicits._
    val dataFrame: DataFrame = List(
      ("hello", 1),
      ("spark", 1),
      ("hello", 1),
      ("flink", 1),
      ("hello", 1),
      ("flink", 1)
    ).toDF("word", "num")
    dataFrame.show()
    //dsl
    val result: DataFrame =dataFrame.groupBy('word).sum("num")
    //sql
    dataFrame.createOrReplaceTempView("t_test")
    val result2=spark.sql("select word,sum(num) from t_test group by word")
    result.show()
    result2.show()
  }
}
